from fastapi import Request

from service import UserService, SongService
from common.result.Result import Result
from common.enum import BaseEnum
from util.log import Log
from util.token import Token
from util.str import Str

async def GetSongSheet(req: Request, sheetId: str):
    try:
        if Str.isBlank(sheetId):
            return Result.error(BaseEnum.PARAMETER_INVALID_ERROR)
        
        # 查歌单
        sheet = await SongService.getSheetById(sheetId)
        if sheet == None:
            return Result.error(BaseEnum.SONG_SHEET_NOT_EXIST_ERROR)

        # 查创建者
        author = await UserService.getUserById(sheet.user_id)
        if author == None:
            return Result.error(BaseEnum.USER_NOT_EXISTS_ERROR)
        
        res = dict(sheet)
        res.update({
            "user": {
                'username': author.username,
                'avatar': author.avatar
            }
        })

        return Result.success(res)
    except Exception as e:
        Log.error('获取 song sheet 列表异常', e)
        return Result.error(BaseEnum.SERVICE_ERROR)